local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script,script.Parent.Base);
local ConnectClass=_G.CommonOHOH.load.Connect();

-- 轮胎
function M.new(...)
	return M(...);
end;

function M.ctor(self,...)--构造函数
	M.super.ctor(self,...);

	--body更新，点位发生变化，自己也得更新
	self.Car.BodyUpdateEvent:Connect(function()
			if self.CarId then
				self:Update(self.CarId)
			end
	end)
end;

function M.dtor(self)
	M.super.dtor(self);
end;
--组装到整车上（子类实现）
function M.Assemble(self)

    self.Body=self.Car:GetBody()
	self.RingModel = Instance.new("Model",self.Body:GetInstance())
	self.RingModel.Name = "Ring"

    -- 获取车的引擎
    local engine=self.Car:GetEngine()


	for i, cf in pairs(self.Body:GetRingPivot()) do
		--美术轮子
		local r= self.Instance:Clone()
		r.Name= tostring(i)
		r:PivotTo(cf)
		r.Parent= self.RingModel 
		--连接轮子和车身
		local attachment0 = Instance.new("Attachment",self.Body:GetInstance())
		attachment0.Name = "ring"..r.Name
		attachment0.WorldCFrame = cf
		
		local attachment1 = Instance.new("Attachment",r)
		attachment1.Name = "ring"..r.Name
		attachment1.WorldCFrame =cf
		
		local hinge = Instance.new("HingeConstraint",self.Body:GetInstance())

		-- 连接两个 Attachment
		hinge.Attachment0 = attachment0
		hinge.Attachment1 = attachment1
        hinge.Name="ring"..r.Name


        local attAO = Instance.new("Attachment",r)
        attAO.Name = "att2engine"

        local alignO  = Instance.new("AlignOrientation",r)
        alignO.Attachment0 = attAO
        alignO.Name="align2engine"
        -- alignO.Attachment1 = engine:GetRingAtt()

		
	end
	
	local radius=self.Instance.Size.Y/2 -- 轮胎半径

    --更新轮胎位置
    engine:UpdateRing(radius)

end

-- 销毁实例
function M.DestroyInstance(self)
	if self.RingModel then
		self.RingModel:Destroy()
		self.RingModel=nil
	end
	
	M.super.DestroyInstance(self);
end


-- 获取组装到车上的实例,返回轮胎数组
function M.GetInstance(self)
	return self.RingModel:GetDescendants()
end
return M